<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>列出 MySQL 服务器中所有的数据库</title>
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-base.css" />
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-medium.css" />

 </head>
 <body class="docs"><div class="navbar navbar-fixed-top">
  <div class="navbar-inner clearfix">
    <ul class="nav" style="width: 100%">
      <li style="float: left;"><a href="function.mysql-insert-id.html">« mysql_insert_id</a></li>
      <li style="float: right;"><a href="function.mysql-list-fields.html">mysql_list_fields »</a></li>
    </ul>
  </div>
</div>
<div id="breadcrumbs" class="clearfix">
  <ul class="breadcrumbs-container">
    <li><a href="index.html">PHP Manual</a></li>
    <li><a href="ref.mysql.html">MySQL 函数</a></li>
    <li>列出 MySQL 服务器中所有的数据库</li>
  </ul>
</div>
<div id="layout">
  <div id="layout-content"><div id="function.mysql-list-dbs" class="refentry">
   <div class="refnamediv">
    <h1 class="refname">mysql_list_dbs</h1>
    <p class="verinfo">(PHP 4, PHP 5)</p><p class="refpurpose"><span class="refname">mysql_list_dbs</span> &mdash; <span class="dc-title">
     列出 MySQL 服务器中所有的数据库
    </span></p>

   </div>
   <div class="refsect1 unknown-unknown-unknown-unknown-unknown-unknown-unknown-seealsv" id="refsect1-function.mysql-list-dbs-unknown-unknown-unknown-unknown-unknown-unknown-unknown-seealsv">
    <h3 class="title">说明</h3>
    <div class="methodsynopsis dc-description">
     <span class="methodname"><strong>mysql_list_dbs</strong></span>(<span class="methodparam"><span class="type">resource</span> <code class="parameter">$link_identifier</code><span class="initializer"> = ?</span></span>): <span class="type">resource</span></div>

    <p class="para rdfs-comment">
     <span class="function"><strong>mysql_list_dbs()</strong></span> 将返回一个结果指针，包含了当前
     MySQL 进程中所有可用的数据库。用
     <span class="function"><a href="function.mysql-tablename.html" class="function">mysql_tablename()</a></span>
     函数来遍历此结果指针，或者任何使用结果表的函数，例如
     <span class="function"><a href="function.mysql-fetch-array.html" class="function">mysql_fetch_array()</a></span>。
    </p>
    <p class="para">
     <div class="example" id="example-1770">
      <p><strong>示例 #1 <span class="function"><strong>mysql_list_dbs()</strong></span> 例子</strong></p>
      <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$link&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">mysql_connect</span><span style="color: #007700">(</span><span style="color: #DD0000">'localhost'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'mysql_user'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'mysql_password'</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$db_list&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">mysql_list_dbs</span><span style="color: #007700">(</span><span style="color: #0000BB">$link</span><span style="color: #007700">);<br /><br />while&nbsp;(</span><span style="color: #0000BB">$row&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">mysql_fetch_object</span><span style="color: #007700">(</span><span style="color: #0000BB">$db_list</span><span style="color: #007700">))&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #0000BB">$row</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">Database&nbsp;</span><span style="color: #007700">.&nbsp;</span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;<br />}<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
      </div>

      <div class="example-contents"><p>
       以上例子将产生如下输出：
      </p></div>
      <div class="example-contents screen">
<div class="cdata"><pre>
database1
database2
database3
...
</pre></div>
      </div>
     </div>
    </p>
    <blockquote class="note"><p><strong class="note">注意</strong>: 
     <p class="para">
      以上代码用起来和
      <span class="function"><a href="function.mysql-fetch-row.html" class="function">mysql_fetch_row()</a></span>
      或类似函数一样简单。
     </p>
    </p></blockquote>
    <p class="para">
     为向下兼容仍然可以使用
     <span class="function"><strong>mysql_listdbs()</strong></span>，但反对这样做。
    </p>
    <p class="para">
     参见 <span class="function"><a href="function.mysql-db-name.html" class="function">mysql_db_name()</a></span>。
    </p>
   </div>

  </div></div></div></body></html>